package main

import (
	"fmt"
	"os"
)

var goal int
var offset = 1000
var num int
var primeCount = 0

func primetask(c chan int) {
	//读取通道中的值
	p := <-c
	if p > goal {
		fmt.Println("prime task uuu : p = ", p)
		os.Exit(0)
	}
	num += 1
	primeCount += 1
	fmt.Println("primetask: 质数  ", p, " primeCount = ", primeCount, " num = ", num)
	//fmt.Println("prime task:", p)
	nc := make(chan int)

	//递归调用
	go primetask(nc)
	for {
		i := <-c //不停地从通道里读数据2,3 4,5 6.....
		if i%p != 0 {
			nc <- i //把i的值传给nc，只有 i是质数的时候才会写入通道
			if i <= 10 {
				fmt.Println("prime task uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu : i % p = (", i, ")%", p, " = ", (i % p), " num = ", num)
			}
		} else {
			fmt.Println("prime task uuu : np - i(", i, ") % p(", p, ") = ", i, "%", p, " = ", (i % p), " num = ", num)
		}
	}
}
func main() {
	//flag.Parse()
	//args := flag.Args()
	//if args != nil && len(args) > 0 {
	//	var err error
	//	goal, err = strconv.Atoi(args[0])
	//	if err != nil {
	//		goal = 100
	//	}
	//} else {
	//	goal = 100
	//}
	goal = 10
	fmt.Println("goal = ", goal)
	c := make(chan int)
	go primetask(c)
	for i := 2; ; i++ {
		//不停地向通道中写整数
		c <- (i)
	}
}
